package com.ihighest.app.infox.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

import com.ihighest.app.common.core.Application;
import com.ihighest.app.infox.bean.CityBean;
import com.ihighest.app.infox.bean.ProcessJson;
import com.ihighest.app.infox.bean.ShengjiBean;
import com.ihighest.app.infox.bean.XianjiBean;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ChooseAddressBaseActivity extends Activity {

    /**
     * 所有省
     */
    public String[] mProvinceDatas;
    /**
     * key - 省 value - 市
     */
    public Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
    /**
     * key - 市 values - 区
     */
    public Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();

    /**
     * key - 市级 values - code
     */
    public Map<String, String> mShijiCodeMap = new HashMap<String, String>();
    /**
     * key - 省级 values - code
     */
    public Map<String, String> mShengJicodeDatasMap = new HashMap<String, String>();

    /**
     * key - 县级 values - code
     */
    public Map<String, String> mXianjicodeDatasMap = new HashMap<String, String>();

    /**
     * 当前省的名称
     */
    public String mCurrentProviceName;
    /**
     * 当前省的code
     */
    public String mCurrentProviceCode;
    /**
     * 当前市的名称
     */
    public String mCurrentCityName;
    /**
     * 当前市的code
     */
    public String mCurrentCityCode;
    /**
     * 当前区的名称
     */
    public String mCurrentDistrictName = "";
    /**
     * 当前区的code
     */
    public String mCurrentDistrictCode = "";

    /**
     * 当前区的邮政编码
     */
    public String mCurrentZipCode = "";


    /**
     * 解析城市数据
     *
     * @param json
     */
    @SuppressWarnings("unchecked")
    public void getCityList(String json) {
        List<CityBean> provinceList = null;
        try {

            // 获取解析出来的数据
            provinceList = getData(json);
            // */ 初始化默认选中的省、市、区
            mCurrentProviceName = provinceList.get(0).getTitle();
            mCurrentProviceCode = provinceList.get(0).getAreacode();
            List<ShengjiBean> ShijiList = provinceList.get(0).getCity();
            if (ShijiList != null && !ShijiList.isEmpty()) {
                mCurrentCityName = ShijiList.get(0).getTitle();
                mCurrentCityCode = ShijiList.get(0).getAreacode();
                List<XianjiBean> xianjiList = ShijiList.get(0).getCounty();
                if (xianjiList != null && !xianjiList.isEmpty()) {
                    mCurrentDistrictName = xianjiList.get(0).getTitle();
                    mCurrentDistrictCode = xianjiList.get(0).getAreacode();
                    mCurrentZipCode = xianjiList.get(0).getAreacode();
                }

            }

            // */
            mProvinceDatas = new String[provinceList.size()];
            for (int i = 0; i < provinceList.size(); i++) {
                // 遍历所有省的数据
                mProvinceDatas[i] = provinceList.get(i).getTitle();
                List<ShengjiBean> shiJiList = provinceList.get(i).getCity();
                String[] shijiNames = null;
                mShengJicodeDatasMap.put(provinceList.get(i).getTitle(), provinceList.get(i).getAreacode());
                if (provinceList.get(i).getCity().size() != 0) {
                    shijiNames = new String[shiJiList.size()];
                    for (int j = 0; j < shiJiList.size(); j++) {
                        // 遍历省下面的所有市的数据
                        shijiNames[j] = shiJiList.get(j).getTitle();
                        String[] xianjiNameArray = null;
                        mShijiCodeMap.put(shiJiList.get(j).getTitle(), shiJiList.get(j).getAreacode());
                        if (shiJiList.get(j).getCounty().size() != 0) {
                            xianjiNameArray = new String[shiJiList.get(j).getCounty().size()];
                            List<XianjiBean> XianjiList = shiJiList.get(j).getCounty();
                            XianjiBean[] xianjiArray = new XianjiBean[XianjiList.size()];
                            for (int k = 0; k < shiJiList.get(j).getCounty().size(); k++) {
                                XianjiBean xianji = new XianjiBean(XianjiList.get(k).getId(), XianjiList.get(k).getTitle(), XianjiList.get(
                                        k).getAreacode());
                                // 区/县对于的邮编，保存到mZipcodeDatasMap
                                mXianjicodeDatasMap.put(XianjiList.get(k).getTitle(), XianjiList.get(k).getAreacode());
                                xianjiArray[k] = xianji;
                                xianjiNameArray[k] = xianji.getTitle();
                            }
                            mDistrictDatasMap.put(shijiNames[j], xianjiNameArray);
                        } else {
                            xianjiNameArray = new String[1];
                            xianjiNameArray[0] = "--";
                            mDistrictDatasMap.put(shijiNames[j], xianjiNameArray);
                        }
                    }
                    // 省-市的数据，保存到mCitisDatasMap
                    mCitisDatasMap.put(provinceList.get(i).getTitle(), shijiNames);
                } else {
                    shijiNames = new String[1];
                    shijiNames[0] = "--";
                    mCitisDatasMap.put(provinceList.get(i).getTitle(), shijiNames);
                }

            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * 获取数据
     *
     * @author lkx Created at 2015-11-26 下午12:03:32
     */
    public List getData(String json) {

        List cityData = ProcessJson.getDate(json);
        return ProcessJson.getDate(json);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Application.getInstance().addActivity(this);
        /* 优化输入法 */
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		/* 友盟push 统计应用启动数据 */
        PushAgent.getInstance(this).onAppStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 友盟移动统计
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        // 友盟移动统计
        if (((Application) getApplication()).mLocationClient != null) {
            ((Application) getApplication()).mLocationClient.stop();
        }
        MobclickAgent.onPause(this);
    }


}
